﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FreeOnlineApplication.Class.Entity;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Collections;

namespace FreeOnlineApplication.Class.Repository
{
    public class FoodRepository
    {
        protected MySqlConnection m_connection;
        MySqlCommand aCommand = new MySqlCommand();

        public FoodRepository(MySqlConnection connection)
        {
            m_connection = connection;
            aCommand.Connection = m_connection;
        }
        public void Insert(Food food)
        {
            string insertSQL = String.Format(@"insert into table_food 
                (
                    food_code,
                    food_name,
                    food_costprice,
                    food_sellprice,
                    unit_id) 
                VALUES ('{0}','{1}',{2},{3},{4})",
                food.CODE, food.NAME, food.COST_PRICE, food.SELL_PRICE, food.UNIT.ID);
            aCommand.CommandText = insertSQL;
            aCommand.ExecuteNonQuery();
            food.ID = GetMaximumId();
        }
        public Food GetById(int id)
        {
            string getByID = String.Format("select * from table_food where food_id = {0}", id);
            aCommand.CommandText = getByID;
            MySqlDataReader aReader = aCommand.ExecuteReader();
            Food food = transform(aReader);
            aReader.Close();
            return food;
        }
        public void Update(Food food)
        {
            string updateSQL = String.Format(@"update table_food set 
                    food_code = '{0}',
                    food_name = '{1}',
                    food_costprice = {2},
                    food_sellprice = {3},
                    unit_id = {4}
                where food_id = {5}",
                food.CODE, food.NAME, food.COST_PRICE, food.SELL_PRICE, food.UNIT.ID, food.ID);
            aCommand.CommandText = updateSQL;
            aCommand.ExecuteNonQuery();
        }
        public void Delete(Food food)
        {
            string deleteSQL = "delete from table_food where food_id = " + food.ID;
            aCommand.CommandText = deleteSQL;
            aCommand.ExecuteNonQuery();
        }
        public int GetMaximumId()
        {
            string getByID = String.Format("SELECT max(food_id) from table_food");
            aCommand.CommandText = getByID;
            object r = aCommand.ExecuteScalar();
            int aReader = r is System.DBNull ? 0 : Convert.ToInt32(r);
            return aReader;
        }
        public int GetCount(int unit)
        {
            string getByID = String.Format("SELECT count(*) from table_food where unit_id = {0}", unit);
            aCommand.CommandText = getByID;
            object r = aCommand.ExecuteScalar();
            int aReader = r is System.DBNull ? 0 : Convert.ToInt32(r);
            return aReader;
        }
        public IList GetAll(int food)
        {
            string sql = String.Format("select * from table_food where unit_id = {0}", food);
            aCommand.CommandText = sql;
            MySqlDataReader aReader = aCommand.ExecuteReader();
            IList users = transformList(aReader);
            aReader.Close();
            return users;
        }
        private Food transform(MySqlDataReader aReader)
        {
            Food food = null;
            while (aReader.Read())
            {
                food = new Food();
                food.ID = Convert.ToInt32(aReader["food_id"]);
                food.CODE = aReader["food_code"].ToString();
                food.NAME = aReader["food_name"].ToString();
                food.UNIT = new Unit(Convert.ToInt32(aReader["unit_id"]));
                food.COST_PRICE = Convert.ToDouble(aReader["food_costprice"]);
                food.SELL_PRICE = Convert.ToDouble(aReader["food_sellprice"]);
            }
            return food;
        }
        private IList transformList(MySqlDataReader aReader)
        {
            IList result = new ArrayList();
            while (aReader.Read())
            {
                Food food = new Food();
                food.ID = Convert.ToInt32(aReader["food_id"]);
                food.CODE = aReader["food_code"].ToString();
                food.NAME = aReader["food_name"].ToString();
                food.UNIT = new Unit(Convert.ToInt32(aReader["unit_id"]));
                food.COST_PRICE = Convert.ToDouble(aReader["food_costprice"]);
                food.SELL_PRICE = Convert.ToDouble(aReader["food_sellprice"]);
                result.Add(food);
            }
            return result;
        }
    }
}
